package com.nut.service.impl;

import com.nut.dao.BookDao;
import com.nut.dao.impl.BookDaoImpl;
import com.nut.pojo.Book;
import com.nut.pojo.Page;
import com.nut.service.BookService;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class BookServiceImpl implements BookService {

    private final BookDao bookDao = new BookDaoImpl();

    @Override
    public void addBook(Book book) {
        bookDao.addBook(book);
    }

    @Override
    public void deleteBookById(Integer id) {
        bookDao.deleteBookById(id);
    }

    @Override
    public void updateBook(Book book) {
        bookDao.updateBook(book);
    }

    @Override
    public Book queryBookById(Integer id) {
        return bookDao.queryBookById(id);
    }

    @Override
    public List<Book> queryBooks() {
        return bookDao.queryBooks();
    }

    /**
     * page
     */
    @Override
    public Page<Book> page(int pageNo, int pageSize) {
        Page<Book> page = new Page<>();
        // 设置每页显示的数量
        page.setPageSize(pageSize);
        // 求总记录数
        //获取总数
        Integer pageTotalCount = bookDao.queryForPageTotalCount();
        // 设置总记录数
        page.setPageTotalCount(pageTotalCount);
        // 求总页码
        int pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        // 设置总页码
        page.setPageTotal(pageTotal);
        // 设置当前页码
        page.setPageNo(pageNo);
        // 求当前页数据的开始索引
        int begin = (page.getPageNo() - 1) * pageSize;
        // 求当前页数据
        List<Book> items = bookDao.queryForItems(begin, pageSize);
        // 设置当前页数据
        page.setItems(items);

        return page;
    }
    /**
     * pageByname
     */
    @Override
    public Page<Book> pageByName(int pageNo, int pageSize,String name) {
        Page<Book> page = new Page<>();
        // 设置每页显示的数量
        page.setPageSize(pageSize);
        // 求总记录数
        Integer pageTotalCount = bookDao.queryForPageTotalCountByName(name);
        // 设置总记录数
        page.setPageTotalCount(pageTotalCount);
        // 求总页码
        int pageTotal = pageTotalCount / pageSize;
        if (pageTotal==0){
            pageTotal+=1;
        }
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        // 设置总页码
        page.setPageTotal(pageTotal);

        // 设置当前页码
        page.setPageNo(pageNo);
        System.out.println("page.getPageNo():"+page.getPageNo());

        // 求当前页数据的开始索引
        int begin = (page.getPageNo() - 1) * pageSize;
        System.out.println("begin:"+begin);
        // 求当前页数据
        List<Book> items = bookDao.queryForItemsByName(name,begin, pageSize);
        System.out.println(items);
        // 设置当前页数据
        page.setItems(items);

        return page;
    }
    @Override
    public void updatePicture(Map map, int id) {
        Book book = queryBookById(id);
        book.setImgPath("http://r3vta9mus.hb-bkt.clouddn.com/"+(String) map.get("fileName"));
        updateBook(book);
    }
    @Override
    public void downloadPicture(int id) {
        Book book = queryBookById(id);
        String path = book.getImgPath();
        String fileName = "new.png";
        FileInputStream fis = null;
        FileOutputStream os = null;
        try {
            fis = new FileInputStream(path);
            String affix = fileName.substring(fileName.lastIndexOf("."));
            String newFileName = System.currentTimeMillis() + affix;
            String desPath = "C:\\Users\\黄楚耀\\Desktop\\" + newFileName;
            os = new FileOutputStream(desPath);
            byte[] bs = new byte[1024];
            int size = 0;
            while ((size = fis.read(bs)) > 0) {
                os.write(bs, 0, size);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
